package com.aplink.generic.widgets;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;

import com.aplink.dellcommercial.R;
import com.aplink.generic.GenericFragment;
import com.aplink.generic.adapter.GenericStateViewPagerAdapter;
import com.aplink.generic.adapter.GenericViewPagerAdapter;
import com.aplink.generic.constant.BundleConstant;
import com.aplink.generic.constant.ConfigParameter;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.ObjPropCol;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.ViewPagerSpec;
import com.aplink.generic.xmlparser.ObjPropInfoParser;

import java.util.ArrayList;

public class CustomViewPager extends BaseView {
    private ViewPager mViewPager;
    private ViewPagerSpec mPagerFormat;

    public CustomViewPager( GenericFragment genericFragment,
                            ControlSpec controlSpec, View view ) {
        super( genericFragment, controlSpec, view );
        mViewPager = (ViewPager) view;
        mPagerFormat = (ViewPagerSpec) getControlSpec();
    }

    @Override
    protected void loadDataToView() {

    }

    @Override
    protected void doIfNotHashPassedValues() {

    }

    @Override
    protected void setEventListener() {

    }

    private void createViewPagerTab() {
        PagerTabStrip pagerTitleStrip = new PagerTabStrip(
                getApplicationContext() );
        ViewPager.LayoutParams layoutParams = new ViewPager.LayoutParams();
        layoutParams.height = ViewPager.LayoutParams.WRAP_CONTENT;
        layoutParams.width = ViewPager.LayoutParams.MATCH_PARENT;
        layoutParams.gravity = Gravity.TOP;
        pagerTitleStrip.setBackgroundColor( getResources().getColor(
                R.color.aplink__generic_viewpager_title_background_color ) );
        int height = (int) getResources().getDimensionPixelSize(
                R.dimen.aplink__generic_viewpager_title_height_color );
        pagerTitleStrip.setPadding( 0, height, 0, height );
        pagerTitleStrip.setTextColor( getResources().getColor(
                R.color.aplink__generic_viewpager_title_text_color ) );
        pagerTitleStrip.setDrawFullUnderline( true );
        pagerTitleStrip.setTabIndicatorColor( getResources().getColor(
                R.color.aplink__generic_viewpager_tab_underline_color ) );
        mViewPager.addView( pagerTitleStrip, layoutParams );
    }

    private void createPagerTitleStrip() {
        PagerTitleStrip pagerTitleStrip = new PagerTitleStrip(
                getApplicationContext() );
        ViewPager.LayoutParams layoutParams = new ViewPager.LayoutParams();
        layoutParams.height = ViewPager.LayoutParams.WRAP_CONTENT;
        layoutParams.width = ViewPager.LayoutParams.MATCH_PARENT;
        layoutParams.gravity = Gravity.TOP;
        pagerTitleStrip.setBackgroundColor( getResources().getColor(
                R.color.aplink__generic_viewpager_title_background_color ) );
        int height = (int) getResources().getDimensionPixelSize(
                R.dimen.aplink__generic_viewpager_title_height_color );
        pagerTitleStrip.setPadding( 0, height, 0, height );
        pagerTitleStrip.setTextColor( getResources().getColor(
                R.color.aplink__generic_viewpager_title_text_color ) );
        mViewPager.addView( pagerTitleStrip, layoutParams );

    }

    protected ArrayList<GenericObject> loadData() {
        ObjPropCol objPropList = ObjPropInfoParser.parseIntoObjProp(
                getApplicationContext(), getContentDataFileId(),
                ConfigParameter.OBJ_PROP_TAGNAME, getControlSpec()
                .getDataObject() );
        return getSelectCommand().getList( objPropList, null );
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void initViewProperties() {
        String headerStyle = mPagerFormat.getHeaderStyle();
        if ( headerStyle != null && headerStyle.equalsIgnoreCase( "tab" ) ) {
            createViewPagerTab();
        } else if ( headerStyle != null && headerStyle.equalsIgnoreCase( "title" ) ) {
            createPagerTitleStrip();
        }
        PagerAdapter genericViewPagerAdapter = null;
        int selectedIndex = 0;
        if ( mPagerFormat.isLoadByConfig() ) {
            genericViewPagerAdapter = new GenericViewPagerAdapter(
                    getChildFragmentManager(), mPagerFormat );
            mViewPager
                    .setOffscreenPageLimit( genericViewPagerAdapter.getCount() );
            selectedIndex = mPagerFormat.getDefaultTargetIndex();
        } else {
            Bundle bundle = getActivity().getIntent().getExtras();
            ArrayList<GenericObject> genericObjects = (ArrayList<GenericObject>) bundle
                    .getSerializable( BundleConstant.LIST_OBJECT );
            selectedIndex = bundle.getInt( BundleConstant.SELECTED_INDEX );
            String fullClassName = mPagerFormat.getDefaultTarget();
            genericViewPagerAdapter = new GenericStateViewPagerAdapter(
                    getFragmentManager(), fullClassName, genericObjects );
            mViewPager.setOffscreenPageLimit( 2 );
        }
        // mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
        mViewPager.setAdapter( genericViewPagerAdapter );
        mViewPager.setCurrentItem( selectedIndex );
    }

    @Override
    public GenericAttribute<?, ?> getAttribute() {
        return null;
    }

    public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
        private static final float MIN_SCALE = 0.85f;
        private static final float MIN_ALPHA = 0.5f;

        public void transformPage( View view, float position ) {
            int pageWidth = view.getWidth();
            int pageHeight = view.getHeight();

            if ( position < - 1 ) { // [-Infinity,-1)
                // This page is way off-screen to the left.
                view.setAlpha( 0 );

            } else if ( position <= 1 ) { // [-1,1]
                // Modify the default slide transition to shrink the page as
                // well
                float scaleFactor = Math.max( MIN_SCALE, 1 - Math.abs( position ) );
                float vertMargin = pageHeight * ( 1 - scaleFactor ) / 2;
                float horzMargin = pageWidth * ( 1 - scaleFactor ) / 2;
                if ( position < 0 ) {
                    view.setTranslationX( horzMargin - vertMargin / 2 );
                } else {
                    view.setTranslationX( - horzMargin + vertMargin / 2 );
                }

                // Scale the page down (between MIN_SCALE and 1)
                view.setScaleX( scaleFactor );
                view.setScaleY( scaleFactor );

                // Fade the page relative to its size.
                view.setAlpha( MIN_ALPHA + ( scaleFactor - MIN_SCALE )
                        / ( 1 - MIN_SCALE ) * ( 1 - MIN_ALPHA ) );

            } else { // (1,+Infinity]
                // This page is way off-screen to the right.
                view.setAlpha( 0 );
            }
        }
    }
}
